package com.bsj.power.job.web;

import cn.hutool.extra.spring.SpringUtil;
import com.bsj.power.common.file.log.OperateLogRead;
import com.bsj.power.common.file.log.OperateLogSaveQueue;
import com.bsj.power.common.file.log.OperateLogWriteAndRead;
import com.bsj.power.job.task.RunAllService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 启动初始化
 * @date 2024/6/3
 */
@Component
@Slf4j
@Import(SpringUtil.class)
public class Action implements CommandLineRunner {

    List<RunAllService> list = new LinkedList<>();

//    @Value("${runService}")
//    private String runServiceBean;

    @Autowired
    private OperateLogWriteAndRead operateLogWriteAndRead;

    @Autowired
    private OperateLogRead operateLogRead;

    /**
     * 初始化
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        //文件存储
        OperateLogSaveQueue.start("operateLog");
        operateLogWriteAndRead.start("operateLog");
        operateLogRead.start("operateLog");
//        if (StringUtils.isNotEmpty(runServiceBean)) {
//            String[] beanSplit = runServiceBean.split(";");
//            for (String bean : beanSplit) {
//                Object beanService = SpringUtil.getBean(bean);
//                if (beanService instanceof RunAllService) {
//                    RunAllService runService = (RunAllService) beanService;
//                    runService.init();
//                    list.add(runService);
//                } else {
//                    log.warn("无法识别的业务名称:" + bean);
//                }
//            }
//        }
    }

    /**
     * 销毁使用的方法
     */
    @PreDestroy
    public void destroy() throws Exception {
        OperateLogSaveQueue.stop();
        operateLogWriteAndRead.stop();
//        try {
//            list.stream().forEach(runService -> {
//                runService.stop();
//            });
//        } catch (Exception e) {
//            log.error("关闭错误: {}", ExceptionUntil.getErrorStr(e));
//        }
        log.info("结束关闭");
    }
}
